/*
 * Project: Sudoku Solver
 * File: sudoku.c
 *
 * Copyright (C) 2009 Daniel Meekins
 * Contact: dmeekins - gmail
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "solve.h"

/* function prototypes */
int fill_nums_from_file(uint32_t nums[ROWS][COLS], char *filename);
void usage(char *name);

/*
 * todo: make this a little more flexible
 */
int fill_nums_from_file(uint32_t nums[ROWS][COLS], char *filename)
{
    FILE *file;
    char *buf;
    uint32_t buf_len;
    uint32_t buf_pos;
    uint32_t i, j;
    
    buf_len = 80;
    
    if((buf = (char *)malloc(sizeof(char) * buf_len)) == NULL)
        return -1;
    
    if((file = fopen(filename, "r")) == NULL)
        return -1;
    
    for(i = 0; i < ROWS; i++)
    {
        if(fgets(buf, buf_len, file) == NULL)
        {
            if(file)
                fclose(file);
            
            return -1;
        }
        
        buf_pos = 0;
        
        for(j = 0; j < COLS; j++)
        {
            sscanf(&buf[buf_pos], "%d", (int*)&nums[i][j]);
            buf_pos += 2; /* skip space and go to next number */
        }
    }
    
    if(file)
        fclose(file);
    
    if(buf)
        free(buf);
    
    return 0;
}

void usage(char *name)
{
    printf("Usage: %s [-d difficulty] file\n", name);
    printf("Options:\n");
    printf("    -d difficulty   Specify the maximum difficulty of the puzzle\n"
           "                    for the program to try to solve. Option can\n"
           "                    be easy, medium, hard, or very-hard.\n"
           "                    Default: hard.\n");
    
    exit(1);
}


int main(int argc, char *argv[])
{
    uint32_t nums[ROWS][COLS];
    char *file_name;
    int difficulty;
    int c;
    
    /* set the default difficulty level */
    difficulty = DIFFICULTY_VERY_HARD; 
    
    opterr = 0;
    
    while((c = getopt(argc, argv, "d:")) != -1)
    {
        switch(c)
        {
            case 'd':
                if(strcmp(optarg, "easy") == 0)
                    difficulty = DIFFICULTY_EASY;
                else if(strcmp(optarg, "medium") == 0)
                    difficulty = DIFFICULTY_MEDIUM;
                else if(strcmp(optarg, "hard") == 0)
                    difficulty = DIFFICULTY_HARD;
                else if(strcmp(optarg, "very-hard") == 0)
                    difficulty = DIFFICULTY_VERY_HARD;
                else
                {
                    fprintf(stderr, "Unknown difficulty \"%s\"\n", optarg);
                    usage(argv[0]);
                }
                break;
                
            default:
                fprintf(stderr, "Unknown option character \'%c\'\n", c);
                usage(argv[0]);
        }
    }
    
    if(optind == argc-1)
    {
        file_name = argv[optind];
    }
    else
    {
        fprintf(stderr, "No filename entered.\n");
        usage(argv[0]);
    }
    
    if(fill_nums_from_file(nums, file_name) != 0)
    {
        fprintf(stderr, "Error getting grid from file\n");
        exit(-1);
    }

    /* do it */
    solve(nums, difficulty);
    
    return 0;
}
